package com.haust.leyou.listener;

import com.haust.leyou.service.GoodsHtmlService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Auther: csp1999
 * @Date: 2020/12/17/17:39
 * @Description: 商品消息监听器(消息消费者)
 */
@Component
public class GoodsListener {

    @Autowired
    private GoodsHtmlService goodsHtmlService;

    /**
     * rabbitmq监听器(新增/修改)
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            // 队列相关配置
            value = @Queue(
                    // 队列名称
                    value = "LEYOU.ITEM.SAVE.QUEUE",
                    // 队列消息持久化
                    durable = "true"
            ),
            // 交换机相关配置
            exchange = @Exchange(
                    // 交换机名称(要和生产方的交换机名称一致)
                    value = "LEYOU.ITEM.EXCHANGE",
                    // 交换机消息持久化
                    durable = "true",
                    // 忽略声明异常
                    ignoreDeclarationExceptions = "true",
                    // 交换机类型：TOPIC
                    type = ExchangeTypes.TOPIC
            ),
            // 消息接收的路由表达式(新增和修改触发的方法相同)
            key = {"item.insert","item.update"}
    ))
    public void save(Long id) {

        // 如果id为null则不做操作
        if (id==null){
            return;
        }

        // 创建缓存的静态页
        goodsHtmlService.createHTML(id);
    }

    /**
     * rabbitmq监听器(删除)
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            // 队列相关配置
            value = @Queue(
                    // 队列名称
                    value = "LEYOU.ITEM.DELETE.QUEUE",
                    // 队列消息持久化
                    durable = "true"
            ),
            // 交换机相关配置
            exchange = @Exchange(
                    // 交换机名称(要和生产方的交换机名称一致)
                    value = "LEYOU.ITEM.EXCHANGE",
                    // 交换机消息持久化
                    durable = "true",
                    // 忽略声明异常
                    ignoreDeclarationExceptions = "true",
                    // 交换机类型：TOPIC
                    type = ExchangeTypes.TOPIC
            ),
            // 消息接收的路由表达式(新增和修改触发的方法相同)
            key = {"item.delete"}
    ))
    public void delete(Long id) {

        // 如果id为null则不做操作
        if (id==null){
            return;
        }

        // 删除缓存的静态页
        goodsHtmlService.deleteHTML(id);
    }
}
